package ar.com.sgsvm.view.cliente;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import ar.com.sgsvm.common.Preferences;


public class DialogOrdenCliente extends DialogFragment {

	private OrdenarClienteListener mOrdenarClienteListener;
	private Preferences preferences;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mOrdenarClienteListener = (OrdenarClienteListener) activity;
		preferences = new Preferences(getActivity());
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		Builder builder = new Builder(getActivity());
		builder.setTitle("Elija una opcion");
		final String[] opciones = new String[] { "id", "apellido", "nombre", "direccion", "localidad", "telefono" };
		int ultimoCriterioSeleccionado = preferences.getCriterioOrdenCliente();
		builder.setSingleChoiceItems(opciones, ultimoCriterioSeleccionado, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(preferences.getCriterioOrdenCliente() == which){
					int toggle = preferences.getAscendenteCliente() * -1;
					preferences.setAscendenteCliente(toggle);
					mOrdenarClienteListener.ordenarClientes(which, toggle);
					
				} else {
					mOrdenarClienteListener.ordenarClientes(which, 1);
				}
				preferences.setCriterioOrdenCliente(which);
				dialog.dismiss();
			}
		});

		return builder.create();
	}

	interface OrdenarClienteListener {
		void ordenarClientes(int which, int asc);
	}

}
